﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ext.Net;
using System.Collections.Generic;

public partial class Popup_Popup_Eqp2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet dsSrouce = oService.Select_DDDW_EQUIP_KIND_S();

                //장비종류
                this.Store3.DataSource = dsSrouce.Tables[0];
                this.Store3.DataBind();

                this.cbxKind.SelectedIndex = 0;

            }

            this.SetListViewData(null, null);
        }

        #region == Determines if the current HttpRequest is an Ext.NET Ajax Request. ==

        if (!X.IsAjaxRequest)
        {

            //분석 필요
            string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed metus nibh, sodales a, porta at, vulputate eget, dui. Pellentesque ut nisl. Maecenas tortor turpis, interdum non, sodales non, iaculis ac, lacus. Vestibulum auctor, tortor quis iaculis malesuada, libero lectus bibendum purus, sit amet tincidunt quam turpis vel lacus. In pellentesque nisl non sem. Suspendisse nunc sem, pretium eget, cursus a, fringilla vel, urna.";

            this.ResourceManager1.RegisterClientScriptBlock("text", string.Format("var text=\"{0}\";", text));

            foreach (Portlet portlet in Ext.Net.Utilities.ControlUtils.FindControls<Portlet>(this.Page))
            {
                if (!portlet.ID.Equals("Portlet1"))
                {
                    portlet.Html = "={text}";
                    portlet.Padding = 5;
                }
            }

        }
        #endregion
    }

    #region == ListView1_SelectionChange(업체선택) ==
    protected void ListView1_SelectionChange(object sender, DirectEventArgs e)
    {
        string[] strParams = this.GetJsonToString(e.ExtraParams["Values"]);

        if (strParams.Length > 1)
        {
            this.hdREGNO.Text = strParams[3];
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_D_EQUIP_3_S(this.cbxKind.Text, this.hdREGNO.Text);

                this.Store2.RemoveAll();

                this.Store2.DataSource = ds.Tables[0];
                this.Store2.DataBind();
            }
        }
    }
    #endregion

    #region == 업체명 리스트뷰 바인드 ==
    protected void SetListViewData(object sender, StoreRefreshDataEventArgs e)
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_D_SELCOMJANGBI_CORP_S(this.tbxCustomer.Text);
                if (ds != null)
                {
                    List<object> data = new List<object>(ds.Tables[0].Rows.Count);
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        data.Add(new
                        {
                            BIZ_REGNO = dr["BIZ_REGNO"].ToString(),
                            COMP_NM = dr["COMP_NM"].ToString(),
                            REPR_NM = dr["REPR_NM"].ToString(),
                        });
                    }

                    this.ListView1.SelectedRow = null;
                    var store = this.ListView1.GetStore();
                    store.DataSource = data;
                    store.DataBind();

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.ListView1.Select(new int[] { 0 });
                    }

                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion


    protected void CreateParameter(object sender, DirectEventArgs e)
    {
        string json = e.ExtraParams["Values"];

        Dictionary<string, string>[] dicValues = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string strVal = string.Empty;

        foreach (Dictionary<string, string> dic in dicValues)
        {
            foreach (KeyValuePair<string, string> row in dic)
            {
                strVal += row.Value + "|";
            }
        }

        strVal = strVal.Remove(strVal.Length - 1);
        hdRowValue.Text = strVal;
    }


    #region == Jason To String ==
    /// <summary>
    /// Json을 string[]으로...
    /// </summary>
    /// <param name="pJson"></param>
    /// <returns></returns>
    private string[] GetJsonToString(string pJson)
    {
        return pJson.Replace("[{", "").Replace(":", "").Replace(",", "").Replace("}]", "").Split(new string[] { "\"" }, StringSplitOptions.RemoveEmptyEntries);
    }
    #endregion


    #region 조회
    protected void PopListSearch(object sender, DirectEventArgs e)
    {
        SetGrid();
    }


    protected void SetGrid()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = null;

            if (cbxKind.Text.Equals("%"))
            {
                this.Store1.RemoveAll();
                this.Store2.RemoveAll();

                ds = oService.Select_D_SELCOMJANGBI_CORP_S(this.tbxCustomer.Text);
                this.Store1.DataSource = ds.Tables[0];
                this.Store1.DataBind();

                ds = oService.Select_D_EQUIP_3_S(this.cbxKind.Text, this.hdREGNO.Text);
                this.Store2.DataSource = ds.Tables[0];
                this.Store2.DataBind();
            }
            else
            {
                ds = oService.Select_D_SELCOMJANGBI_CORP_S2(this.tbxCustomer.Text, this.cbxKind.Value.ToString());
                this.Store1.DataSource = ds.Tables[0];
                this.Store1.DataBind();

                this.Store2.RemoveAll();
            }

            this.Store1.DataSource = ds;
            this.Store1.DataBind();
        }
    }
    #endregion


    protected void btnInser_Click(object sender, DirectEventArgs e)
    {
        var win = new Window
        {
            ID = "Window2",
            //Title = "My Window",
            Height = 200,
            Width = 700,
            Padding = 5
        };


        Ext.Net.Button btn = (Ext.Net.Button)sender;
        string id = btn.ID;

        switch (id)
        {
            case "btnBizInst":
                win.Listeners.Hide.Handler = "btnBizInst.setDisabled(false);";
                win.AutoLoad.Url = "Popup_Biz.aspx?GUBUN=2";
                win.Title = "업체등록";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;

            case "btnBizUdt":
                win.Listeners.Hide.Handler = "btnBizUdt.setDisabled(false);";
                win.AutoLoad.Url = "Popup_Biz.aspx?REGNO=" + this.hdREGNO.Text + "&GUBUN=2";
                win.Title = "업체수정/삭제";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;

            case "btnEqpInst":
                win.Listeners.Hide.Handler = "btnEqpInst.setDisabled(false);";
                win.AutoLoad.Url = "Popup_EqpFind.aspx?REGNO=" + this.hdREGNO.Text;
                win.Title = "장비등록";
                win.Height = 350;
                win.Width = 350;
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;

            default:
                break;
        }
    }
}
